/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MDTPDecoder.h"

#include <Winsock2.h>

#include "MAddressMAC.h"

//---------------------------------------------------------------------------

MDTPDecoder::MDTPDecoder(void)
{
}

MDTPDecoder::~MDTPDecoder(void)
{
}

//---------------------------------------------------------------------------

CString MDTPDecoder::GetTypeName(WORD wIn)
{
	CString str;

	switch(wIn){
		case DTPT_MAC:
			str = _T("MAC");
			break;

		default:
			str.Format(_T("Type %u"), wIn);
			break;
	}

	return str;
}

//---------------------------------------------------------------------------

CString MDTPDecoder::FormatRawData(BYTE* dataIn, int sizeIn)
{
	CString str, sss;
	int i, size;

	size = sizeIn;
	if(size>128){
		str = _T("[hex, truncated] ");
		size = 128;
	}
	else{
		str = _T("[hex] ");
	}

	if(size==1){
		str.Format(_T("0x%02X"), dataIn[0]);
		return str;
	}

	for(i=0; i<size; i++){
		if(i<(size-1)) sss.Format(_T("%02X "), dataIn[i]);
		else           sss.Format(_T("%02X"), dataIn[i]);
		str += sss;
	}

	return str;
}

CString MDTPDecoder::FormatMac(BYTE* dataIn, int sizeIn)
{
	MAddressMAC addrMac;
	CString str;

	if(sizeIn!=6){
		str.Format(_T("- invalid length (%u) -"), sizeIn);
		return str;
	}

	addrMac.LoadFromBytePtr(dataIn);
	str = addrMac.GetS();
	return str;
}

CString MDTPDecoder::GetValueName(MTLVRecord* recIn)
{
	CString str;
	int size;

	str.Empty();
	if(recIn==NULL) return str;

	size = recIn->xLength;
	if(size<4){
		str.Format(_T("- invalid length %u -"), size);
		return str;
	}

	size -= 4;
	if(size==0){
		str = _T("- empty -");
		return str;
	}

	switch(recIn->xType){
		case DTPT_MAC:
			str = FormatMac(recIn->xValue, size);
			break;

		default:
			str = FormatRawData(recIn->xValue, size);
			break;
	}

	return str;
}

//---------------------------------------------------------------------------

CString MDTPDecoder::GetMacTypeValue(MDTPPacket* packIn)
{
	MTLVRecord *rec;
	int i, cnt;
	CString str;
	bool found;

	if(packIn==NULL){
		str.Empty();
		return str;
	}

	cnt = packIn->xValuesList.GetSize();
	if(cnt==0){
		str.Empty();
		return str;
	}

	found = false;
	for(i=0; i<cnt && !found; i++){
		rec = packIn->xValuesList.GetByIdx(i);
		if(rec!=NULL){
			if(rec->xType==DTPT_MAC){
				found = true;
				str = GetValueName(rec);
			}
		}
	}

	if(!found)
		str.Empty();

	return str;
}

//---------------------------------------------------------------------------
